﻿using SienSchoofsProject.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace SienSchoofsProject.Controllers
{
    [Authorize(Roles = "admin")]
    public class ChangeRoleController : BaseController
    {
        private FilmDb _db = new FilmDb();

        [HttpGet]
        public ActionResult Index()
        {
            try
            {
                var model = TempData["RolesModel"];
                if (model == null)
                {
                    var RolesList = new List<string>();
                    var RolesQuery = Roles.GetAllRoles();
                    RolesList.AddRange(RolesQuery.Distinct());
                    ViewBag.roles = new SelectList(RolesList);


                    ViewBag.Username = TempData["User"];

                    ViewBag.Rol = Resources.Resources.NoRoleYet;
                    return View();
                }
                else
                {
                    var RolesList = new List<string>();
                    var RolesQuery = Roles.GetAllRoles();
                    RolesList.AddRange(RolesQuery.Distinct());
                    ViewBag.roles = new SelectList(RolesList);

                    ViewBag.Username = TempData["User"];
                    ViewBag.Rol = model;
                    return View();
                }

            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
            }

            return View();
        }

        [HttpPost]
        [MultiButton(MatchFormKey = "action", MatchFormValue = "Voeg rol toe")]
        public ActionResult Feedback(string roles, string username)
        {
            try
            {
                if (!string.IsNullOrEmpty(roles) || !roles.Equals(""))
                {

                    if (!string.IsNullOrEmpty(username))
                    {
                        try
                        {
                            Roles.AddUserToRole(username, roles);
                            ViewData["title"] = Resources.Resources.RoleApplTitle;
                            ViewData["header"] = Resources.Resources.RoleApplHead;
                            ViewData["message"] = "";
                            return View();
                        }
                        catch (Exception)
                        {
                            ViewData["title"] = Resources.Resources.RoleErrorTitle;
                            ViewData["header"] = Resources.Resources.RoleAlreadHead;
                            ViewData["message"] = "";
                            return View();

                        }
                    }
                    else
                    {
                        ViewData["title"] = Resources.Resources.RoleErrorTitle;
                        ViewData["header"] = Resources.Resources.RoleErrorHead;
                        ViewData["message"] = "";
                        return View();
                    }
                }
                else
                {
                    ViewData["title"] = Resources.Resources.RoleErrorTitle;
                    ViewData["header"] = Resources.Resources.RoleErrorHead;
                    ViewData["message"] = Resources.Resources.RoleNotSpecMsg;
                    return View();
                }
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
            }

            return View();
        }
        [HttpPost]
        [MultiButton(MatchFormKey = "action", MatchFormValue = "Verwijder rol")]
        public ActionResult FeedbackDelete(string roles, string username)
        {
            try
            {
                if (!string.IsNullOrEmpty(roles) || !roles.Equals(""))
                {

                    if (!string.IsNullOrEmpty(username))
                    {
                        try
                        {
                            Roles.RemoveUserFromRole(username, roles);
                            ViewData["title"] = Resources.Resources.RoleDelSucTitle;
                            ViewData["header"] = Resources.Resources.RoleDelSucHead;
                            ViewData["message"] = "";
                            return View();
                        }
                        catch (Exception)
                        {
                            ViewData["title"] = Resources.Resources.RoleDelFailTitle;
                            ViewData["header"] = Resources.Resources.RoleDelFailSpecHead;
                            ViewData["message"] = "";
                            return View();

                        }
                    }
                    else
                    {
                        ViewData["title"] = Resources.Resources.RoleDelFailTitle;
                        ViewData["header"] = Resources.Resources.RoleDelFailHead;
                        ViewData["message"] = "";
                        return View();
                    }
                }
                else
                {
                    ViewData["title"] = Resources.Resources.RoleDelFailTitle;
                    ViewData["header"] = Resources.Resources.RoleDelFailHead;
                    ViewData["message"] = Resources.Resources.RoleDelFailMsg;
                    return View();
                }
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
            }

            return View();
        }
    }
}
